﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace LAPI.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Home", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //Configuramos log4net
            var logcfg = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
            LAPI.Core.Logging.LoggerFactory.ConfigFile = logcfg;
            //Configuramos gestorbbdd
            LAPI.Data.GestorBBDDFactory.CadenaConexion = System.Configuration.ConfigurationManager.ConnectionStrings["LAPI"].ConnectionString;
            //Configuramos clientesmtp
            LAPI.Core.Mail.ClientSMTPFactory.credenciales = new System.Net.NetworkCredential(
                userName: System.Configuration.ConfigurationManager.AppSettings["smtp.user"],
                password: System.Configuration.ConfigurationManager.AppSettings["smtp.pwd"]);
            LAPI.Core.Mail.ClientSMTPFactory.host = System.Configuration.ConfigurationManager.AppSettings["smtp.host"];
            LAPI.Core.Mail.ClientSMTPFactory.puerto = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["smtp.port"]);
            LAPI.Core.Mail.ClientSMTPFactory.ssl = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["smtp.sslenabled"]);

        }

        protected void Application_Error()
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;
            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values["controller"] = "Errors";
            routeData.Values["action"] = "General";
            routeData.Values["exception"] = exception;
            Response.StatusCode = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                    case 403:
                        routeData.Values["action"] = "Http403";
                        break;
                    case 404:
                        routeData.Values["action"] = "Http404";
                        break;
                }
            }
            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
            IController errorsController = new LAPI.Web.Controllers.Errores.ErrorsController();
            HttpContextWrapper wrapper = new HttpContextWrapper(Context);
            var rc = new RequestContext(wrapper, routeData);
            errorsController.Execute(rc);
        }
    }
}